package com.yogurt.cms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yogurt.cms.entity.CrmBanner;
import com.yogurt.cms.service.CrmBannerService;
import com.yogurt.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author yogurt
 * @since 2020-12-13
 */
@RestController
@RequestMapping("/educms/banner")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    @ApiOperation("分页查询banner列表")
    @GetMapping("/page/{currentPage}/{size}")
    public Result pageBanner(@PathVariable long currentPage,@PathVariable long size){
        Page<CrmBanner> page = new Page<>(currentPage,size);
        bannerService.page(page);
        return Result.ok().data("records",page.getRecords()).data("total",page.getTotal());
    }

    @ApiOperation("添加banner")
    @PostMapping("/add")
    public Result addBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = bannerService.save(crmBanner);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @ApiOperation("修改banner")
    @PutMapping("/update")
    public Result updateBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = bannerService.updateById(crmBanner);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    @ApiOperation("根据id查询banner")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable String id){
        CrmBanner crmBanner = bannerService.getById(id);
        if (crmBanner != null){
            return Result.ok().data("item",crmBanner);
        }else {
            return Result.error();
        }
    }

    @ApiOperation("删除banner")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable String id){
        boolean flag = bannerService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

}

